using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using Engine.Terrain;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace HeightMapGenerator
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class HeightMapGenerator : Microsoft.Xna.Framework.Game
    {
        private readonly string HeightMapFileName;
        private readonly string TerrainOutputFileName;
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public HeightMapGenerator(string heightMapFileName,string terrainOutputFileName)
        {
            HeightMapFileName = heightMapFileName;
            TerrainOutputFileName = terrainOutputFileName;
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            HeightMapManager heightMapManager=new HeightMapManager();
            HeightMapParameters parameters=new HeightMapParameters();
            parameters.BlockSize = 30;
            parameters.HeightScale = 100;
            parameters.TextureScale = 0.1f;
            
            var terrain = heightMapManager.GenerateHeightMapFromFile(this, parameters, HeightMapFileName);
            BinaryFormatter binaryFormatter=new BinaryFormatter();
            using(Stream stream=File.Open(TerrainOutputFileName,FileMode.Create))
            {
                binaryFormatter.Serialize(stream, terrain);
            }
            Exit();
            base.Initialize();
        }
    }
}
